﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using Estimate.Common.Repository;
using Estimate.Common.Entity;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Estimate;

namespace Estimate.Common.DBHealth
{
    public class SiteForeHealth
    {
        public bool CheckLackRecord = true;
        public bool CheckEmptySemi = true;
        public bool CheckRepeatStation = true;
        public bool CheckLackStation = true;
        public bool CheckAbnormalStation = true;
        public bool CheckValue = true;
        public bool CheckSemi = true;

        private List<string> STForecasters = new List<string>();//所有省台预报员
        private List<string> NeedRemoveTables = new List<string>() { "_SiteFore", "Jiangsu_SiteFore", "Schedule_SiteFore", "St_SiteFore" };
        
       
        DateTime BeginDate;
        DateTime EndDate;
        string ConnectionString;

        private Dictionary<string, int> _autoStations;
        private Dictionary<string, int> _manualStations;

        public SiteForeHealth()
        {
            ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];

            _autoStations = new Dictionary<string, int>();
            _manualStations = new Dictionary<string, int>();

            //所有省台预报员数据表名
            List<string> SiteForeTables = SiteForeRepository.GetSiteForeTables(ConnectionString);
            foreach (string table in SiteForeTables)
            {
                if (!NeedRemoveTables.Contains(table))
                    STForecasters.Add(table.Substring(0, table.IndexOf('_')));
            }
        }

        public DataTable CheckHealth(DateTime beginDate, DateTime endDate, string dir)
        {
            List<DateTime> Dates = new List<DateTime>();
            List<int> Years = new List<int>();
            DateTime cur = beginDate;
            int year = 0;
            while (cur <= endDate)
            {
                if (cur.Year != year)
                {
                    year = cur.Year;
                    Years.Add(year);
                }
                Dates.Add(cur);
                cur = cur.AddDays(1);
            }
           

            return null;
        }

        public DataTable CheckHealth(List<DateTime> Dates, List<int> Years, string forecaster, string dir,ForeSemiType semitype)
        {
            Dictionary<DateTime, Dictionary<ForeClockType, ForeSpanType>> Files = new Dictionary<DateTime, Dictionary<ForeClockType, ForeSpanType>>();
            foreach (int year in Years)
            {
                foreach (string file in Directory.GetFiles(String.Format(@"{0}\{1}\zdzd\{2}", dir, forecaster, year)))
                {
                    FileInfo f = new FileInfo(file);
                    DateTime date = DateTime.Parse(year.ToString() + f.Name.Substring(2, 4));
                    if (Dates.Contains(date))
                    {
                        ForeClockType clock = (ForeClockType)Enum.Parse(typeof(ForeClockType), "H" + f.Name.Substring(6, 2));
                        ForeSpanType span = (ForeSpanType)Enum.Parse(typeof(ForeSpanType), "FD00_" + f.Name.Substring(10, 2));//有问题
                        //string str1= f.Name.Substring(10, 2);
                        //if(str=="")
                        //(ForeSpanType)Enum.Parse(typeof(ForeSpanType), "FD00_" +);

                        if (!Files.ContainsKey(date))
                            Files.Add(date, new Dictionary<ForeClockType, ForeSpanType>());
                        Files[date].Add(clock, span);
                    }
                }
                
            }

            ForeRepository Repository = ForeRepositoryFactory.SiteForeRepository(ConnectionString, forecaster);
            List<ForeClockType> clocks = new List<ForeClockType>();
            List<ForeSpanType> spans = new List<ForeSpanType>();
            foreach (KeyValuePair<DateTime, Dictionary<ForeClockType, ForeSpanType>> pair in Files)
            {
               clocks.AddRange( pair.Value.Keys.ToArray<ForeClockType>());
               spans.AddRange(pair.Value.Values.ToArray<ForeSpanType>());
            }

            ForeEntity[] entities = Repository.InquerySemiWithDateRange(BeginDate, EndDate, clocks.ToArray(), spans.ToArray(), semitype);

            return null;
        }
    }
}
